<?php
namespace app\applet\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class ShopsTemplate extends Model
{
    /**
     * 商铺模板管理
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateList(){
        //后台朝向数据
        $date=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('housing_oorientation')
            ->find();
        //配套
        $i=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('shop_matching')
            ->find();
        //var_dump($data,$date);exit;
        //判断设置没有   没有返回2
        if($date!=null && $i!=null){
            $arr['orientation']=$date['housing_oorientation'];//朝向
            $arr['matching']=$i['shop_matching'];//配套
            return $arr;
        }else{
            return 2;
        }
    }
    /**
     * 商铺房源模板添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateAdd($data){
        //添加操作用户id
        /*$uid=get_user_id($data['token']);
        $arr['te_uid'] = $uid;*/
        //处理数据
        $arr['s_te_uid'] = 2;
        $arr['s_te_name'] = isset($data['s_te_name']) ? trim($data['s_te_name']) : "";
        $arr['s_te_area'] = isset($data['s_te_area']) ? trim($data['s_te_area']) : "";
        $arr['s_te_money'] = isset($data['s_te_money']) ? trim($data['s_te_money']) : "";
        $arr['s_te_face_widt'] = isset($data['s_te_face_widt']) ? trim($data['s_te_face_widt']) : "";
        $arr['s_te_layer_depth'] = isset($data['s_te_layer_depth']) ? trim($data['s_te_layer_depth']) : "";
        $arr['s_te_depth'] = isset($data['s_te_depth']) ? trim($data['s_te_depth']) : "";
        $arr['s_te_orientation'] =  isset($data['s_te_orientation']) ? trim($data['s_te_orientation']) : "";
        $arr['s_te_floor_plans'] = isset($data['s_te_floor_plans']) ? implode(',',$data['s_te_floor_plans']) : "";
        $arr['s_te_indoor_drawings'] = isset($data['s_te_indoor_drawings']) ? implode(',',$data['s_te_indoor_drawings']) : "";
        $arr['s_te_matching'] = isset($data['s_te_matching']) ? implode(',',$data['s_te_matching'])  : "";
        $arr['s_te_a_time'] = time();
        $data=Db::name('shops_template')->insert($arr);
        return $data;
    }


    /**
     * 商铺资源模板查询
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templategOne($id){
        //处理数据
        $data=Db::name('shops_template')
            ->where(['shops_t_id'=>$id])
            ->where(['s_te_state'=>1])
            ->find();
        $data['s_te_floor_plans']=explode(',',$data['s_te_floor_plans']);
        $data['s_te_indoor_drawings']=explode(',',$data['s_te_indoor_drawings']);
        $data['s_te_matching']=explode(',',$data['s_te_matching']);
        //var_dump($data);exit;
        return $data;
    }
    /**
     * 商铺模板修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateUpdate($data){
        //处理数据
        $where['shops_t_id']=$data['shops_t_id'];
        //处理数据
        $arr['s_te_name'] = isset($data['s_te_name']) ? trim($data['s_te_name']) : "";
        $arr['s_te_area'] = isset($data['s_te_area']) ? trim($data['s_te_area']) : "";
        $arr['s_te_money'] = isset($data['s_te_money']) ? trim($data['s_te_money']) : "";
        $arr['s_te_face_widt'] = isset($data['s_te_face_widt']) ? trim($data['s_te_face_widt']) : "";
        $arr['s_te_layer_depth'] = isset($data['s_te_layer_depth']) ? trim($data['s_te_layer_depth']) : "";
        $arr['s_te_depth'] = isset($data['s_te_depth']) ? trim($data['s_te_depth']) : "";
        $arr['s_te_orientation'] =  isset($data['s_te_orientation']) ? trim($data['s_te_orientation']) : "";
        $arr['s_te_floor_plans'] = isset($data['s_te_floor_plans']) ? implode(',',$data['s_te_floor_plans']) : "";
        $arr['s_te_indoor_drawings'] = isset($data['s_te_indoor_drawings']) ? implode(',',$data['s_te_indoor_drawings']) : "";
        $arr['s_te_matching'] = isset($data['s_te_matching']) ? implode(',',$data['s_te_matching']) : "";
        $arr['s_te_u_time'] = time();
        $data=Db::name('shops_template')->where($where)->update($arr);
        return $data;
    }
    /**
     * 商铺模板显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index(){
        //处理数据
        $where['s_te_state']=1;
        $data=Db::name('shops_template')
            ->alias('a')
            ->join('admin_user','a.s_te_uid=admin_user.admin_id')
            ->field('shops_t_id,s_te_name,s_te_face_widt,s_te_layer_depth,s_te_depth,s_te_a_time,name')
            ->where($where)->select();
        return $data;
    }
    /**
     * 商铺模板删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateDel($data){
        //处理数据
        $where['shops_t_id']=$data['shops_t_id'];
        $data=Db::name('shops_template')
            ->where($where)
            ->update(['s_te_state'=>2]);
        return $data;
    }

    /**单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unit(){
        //单元楼数据
        $data=Db::name('unit_building')
            ->field('tung_num,unit_num,layer_num,floor_type')
            ->where(['u_type'=>1])
            ->order('tung_num asc,unit_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['tung_num'].'-'.$v['unit_num'];
            }
        }
        return $data;
    }
    /**单元楼模板显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsTemplate($id){
        //单元楼数据
        $data=Db::name('shops_unit')
            ->field('s_u_id,tung_s_num,unit_s_num,layer_s_num')
            ->where(['te_s_state'=>1])
            ->where(['t_s_id'=>$id])
            ->order('tung_s_num asc,unit_s_num asc,layer_s_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['tung']=$v['tung_s_num'].'栋'.$v['unit_s_num'].'单元'.$v['layer_s_num'].'层';
            }
        }
        return $data;
    }
    /**单元楼和模板关联添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsTemplateAdd($data){
        $arr['tung_s_num'] = isset($data['tung_s_num']) ? trim($data['tung_s_num']) : "";
        $arr['unit_s_num'] = isset($data['unit_s_num']) ? trim($data['unit_s_num']) : "";
        $arr['layer_s_num'] = isset($data['layer_s_num']) ? trim($data['layer_s_num']) : "";
        $datas=Db::name('shops_unit')->where($arr)->find();
        //判断楼层是否已被关联
        if($datas!=null){
            return 2;
        }
        $arr['t_s_id'] = isset($data['t_s_id']) ? trim($data['t_s_id']) : "";
        if($arr['t_s_id']==null){
            return 2;
        }
        $arr['s_a_time'] = time();
        //单元楼数据
        $data=Db::name('shops_unit')->insert($arr);
       // $datas=Db::name('shops_unit')->where($arr)->select();
        return $data;
    }
    /**单元楼和模板关联删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitTemplateDel($id){
        //单元楼数据
        $data=Db::name('shops_unit')
            ->field('t_s_id')
            ->where(['s_u_id'=>$id])
            ->find();
        $datas=Db::name('shops_unit')
            ->where(['s_u_id'=>$id])
            ->delete();
        $data=Db::name('shops_unit')
            ->where(['t_s_id'=>$data['t_s_id']])
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['tung']=$v['tung_s_num'].'栋'.$v['unit_s_num'].'单元'.$v['layer_s_num'].'层';
            }
        }
        return $data;
    }
    /**选择单元楼楼层返回模板
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function TemplateChoice($data){
        //单元楼数据
        $arr['tung_s_num'] = isset($data['tung_s_num']) ? trim($data['tung_s_num']) : "";
        $arr['unit_s_num'] = isset($data['unit_s_num']) ? trim($data['unit_s_num']) : "";
        $arr['layer_s_num'] = isset($data['layer_s_num']) ? trim($data['layer_s_num']) : "";
        $datas=Db::name('shops_unit')
            ->field('t_s_id')
            ->where($arr)->find();
        $date=Db::name('shops_template')
            ->where(['shops_t_id'=>$datas['t_s_id']])->find();
        //判断楼层是否已被关联
        return $date;
    }
    /**选择单元楼楼层返回模板
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function TemplateSe($id){
        //单元楼数据
        $date=Db::name('shops_template')
            ->where(['shops_t_id'=>$id])->find();
        //判断楼层是否已被关联
        return $date;
    }
}